<template>
  <div class="all" @click="clickAll()" ref="all">
    <header>
      <div class="div" @click="back()">
        <img src="../../assets/返回.png" alt="">
        <span>返回</span>
      </div>
      <div class="div">
        <span>朋友圈</span>
      </div>
      <div class="div" @click="clickSend()">
        <img src="../../assets/蓝添加.png" alt="">
        <span>发布</span>
      </div>
    </header>
    <div class="content">
      <van-empty
        v-show="circleArr.length <= 0"
        class="custom-image"
        style="width: 100%;height: 400px;"
        :image="require('../../assets/空两本书.png')"
        description="暂无朋友圈信息"
      />
      <div class="list" v-for="(item, index) in circleArr" :key="index">
        <div class="left_img">
          <van-image
            v-if="item.PHOTO != undefined && item.PHOTO.length>0 && item.PHOTO != ''"
            @click="clickpeoInfo(item.user_id)"
            round
            fit="cover"
            width="44px"
            height="44px"
            :src="item.PHOTO != undefined && item.PHOTO.substring(0, 4) === 'http' ? item.PHOTO : titleImg + item.PHOTO"
          />
          <van-image
            v-else-if="item.PHOTO == undefined || item.PHOTO == ''"
            @click="clickpeoInfo(item.user_id)"
            round
            width="44px"
            height="44px"
            fit="cover"
            :src="require('../../assets/男患者@2x.png')"
          />
        </div>
        <div class="right_content">
          <p class="name">{{item.NICK_NAME}}</p>
          <p class="text">{{item.content}}</p>
          <ul>
            <li v-for="(imgItem, imgIndex) in item.img"
                @click="clickImg(item.img, imgIndex)"
                :key="imgIndex">
              <van-image
                width="100%"
                height="100%"
                fit="cover"
                :src="item.PHOTO.substring(0, 4) === 'http' ? imgItem.img_yasuo : imgHttp + imgItem.img_yasuo"
              />
            </li>
          </ul>
          <p class="time">{{item.created_at}}</p>
          <p class="mune">
<!--            <span  v-show="item.dianzan == 0" @click="Click(item)">-->
<!--              <img src="../../assets/点赞未@2x.png"  alt="" >-->
<!--              点赞-->
<!--            </span>-->
<!--            <span @click="isClick(item)" v-show="item.dianzan == 1">-->
<!--              <img src="../../assets/点赞@2x.png"   alt="">-->
<!--              点赞-->
<!--            </span>-->
            <span v-if="Number(item.relationship) === 1" @click="clickDel(item, index)">删除</span>
            <span @click.stop="isShow(item, item.comment)">
              <img src="../../assets/评论@2x.png" alt="">
              评论
            </span>
          </p>
          <div class="comment" v-show="item.comment.length > 0">
            <p v-for="(msgItem, msgIndex) in item.comment" :key="msgIndex">
<!--              <img class="pinglunImg" :src="msgItem.PHOTO" alt="">-->
              <span class="comment_name" @click="clickpeoInfo(msgItem.comment_user_id)">{{msgItem.NICK_NAME}}：</span>
              {{msgItem.msg}}
            </p>
          </div>
        </div>
      </div>
      <p class="pingLun" v-show="show" @click.stop="">
        <van-search
          v-model="value"
          shape="round"
          background="#fff"
          placeholder="说点什么吧~"
          left-icon=""
          autofocus
          @focusout="inputFocusout()"
        >
          <template #action>
          </template>
        </van-search>
        <span class="plSend" @click="clickFaSong()">发送</span>
      </p>
      <van-image-preview v-model="imgShow"
                         :start-position="startPosition"
                         showIndex
                         :images="images" @change="onChange">
        <template v-slot:index>第{{ startPosition + 1 }}页</template>
      </van-image-preview>
      <van-dialog v-model="delShow"
                  width="80%"
                  confirm-button-color="#1da4f2"
                  title="确认删除吗？"
                  @confirm="delConfirm()"
                  show-cancel-button>
      </van-dialog>
    </div>
  </div>
</template>

<script>
import videoHttp from '../../http/video.http'
import $ from 'jquery'
import {Toast} from 'vant'
export default {
  name: 'GroupFriends',
  data () {
    return {
      show: false,
      delShow: false,
      value: '',
      userid: '',
      page: 1,
      imgHttp: videoHttp.friendsImg, // 图片前缀
      titleImg: videoHttp.imgHttp, // 头像图片前缀
      circleArr: [],
      items: 20,
      item: '',
      imgShow: false,
      index: 1,
      startPosition: 0,
      images: [],
      delItem: '',
      delIndex: 0,
      isScroll: true,
      isFaSongSuccess: true
    }
  },
  mounted () {
    // 截取网址后的userid
    this.userid = this.$utils.getUrlKey('userid')
    // 获取朋友圈列表
    this.getCircleList()
    window.addEventListener('scroll', this.scrollBottom, true)
  },
  methods: {
    inputFocusout () {
      this.timer = setTimeout(() => {
        window.scrollTo(0, 0)
        // 间隔设为10，减少页面失去焦点定时器的突兀感，
      }, 10)
    },
    // 点击删除
    clickDel (item, index) {
      this.delShow = true
      this.delItem = item
      this.delIndex = index
    },
    // 删除确定按钮
    delConfirm () {
      this.$http.updateCircleActionOne({id: this.delItem.id})
        .then(res => {
          console.log(res)
          this.circleArr.splice(this.delIndex, 1)
          Toast('删除成功')
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 滚动事件
    scrollBottom () {
      let scrollTop = this.$refs.all.scrollTop
      let clientHeight = this.$refs.all.clientHeight
      let scrollHeight = this.$refs.all.scrollHeight
      if (scrollHeight - scrollTop <= clientHeight + 300 && this.isScroll === true) {
        this.isScroll = false
        setTimeout(() => {
          this.page += 1
          // 移除滚动事件
          window.removeEventListener('scroll', this.scrollBottom, true)
          this.getCircleList()
        }, 50)
      }
    },
    throttle (func, wait) {
      let lastTime = null
      let timeout
      return () => {
        let context = this
        let now = new Date()
        let arg = arguments
        if (now - lastTime - wait > 0) {
          if (timeout) {
            clearTimeout(timeout)
            timeout = null
          }
          func.apply(context, arg)
          lastTime = now
        } else if (!timeout) {
          timeout = setTimeout(() => {
            func.apply(context, arg)
          }, wait)
        }
      }
    },
    // 图片展示
    onChange (index) {
      this.index = index
      this.startPosition = index
    },
    // 点击图片
    clickImg (imgItem, imgIndex) {
      this.imgShow = !this.imgShow
      this.images = []
      for (var i = 0; i < imgItem.length; i++) {
        if (imgItem[i].img.substring(0, 4) === 'http') {
          this.images.push(imgItem[i].img)
        } else {
          this.images.push(this.imgHttp + imgItem[i].img)
        }
      }
      this.startPosition = imgIndex
    },
    // 点击其他位置关闭
    clickAll () {
      this.show = false
    },
    // 点击查看个人详情
    clickpeoInfo (id) {
      this.$router.push({path: '/detailsFriend', query: {userid: id}})
    },
    // 点击发送
    clickFaSong () {
      if (this.value.length > 0) {
        if (this.isFaSongSuccess === false) {
          Toast('您点的太快了，请稍后再评论')
          return false
        }
        this.isFaSongSuccess = false
        this.$http.insertCircleActionComment({
          action_id: this.item.id,
          comment_user_id: this.userid,
          msg: this.value
        })
          .then(res => {
            console.log(res)
            this.show = false
            var obj = {
              NICK_NAME: res.data.data.NICK_NAME,
              PHOTO: res.data.data.PHOTO,
              comment_user_id: res.data.data.USER_ID,
              msg: this.value
            }
            this.isFaSongSuccess = true
            this.item.comment.push(obj)
            this.value = ''
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        Toast('请输入内容哦')
      }
    },
    // 点赞
    Click (item) {
      this.$http.insertCircleActionPraise({
        action_id: item.id,
        user_id: this.userid
      })
        .then(res => {
          console.log(res)
          if (res.data.status === 'success') {
            item.dianzan = 1
          } else {
            Toast('网络异常，请重试')
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 取消点赞
    isClick (item) {
      this.$http.insertCircleActionPraise({
        action_id: item.id,
        user_id: this.userid
      })
        .then(res => {
          console.log(res)
          if (res.data.status === 'success') {
            item.dianzan = 0
          } else {
            Toast('网络异常，请重试')
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 点击评论
    isShow (item) {
      this.show = !this.show
      this.item = item
      if (this.show === true) {
      // this.focus()
      // 输入框自动获取焦点
        this.$nextTick(() => {
          // $('#input').focus()
          console.log($('.van-search input').focus())
        }, 100)
      }
    },
    // 返回
    back () {
      this.$router.history.go(-1)
      window.removeEventListener('scroll', this.scrollBottom, true)
    },
    // 请求朋友圈数据
    getCircleList () {
      this.$http.findCircleActionList({user_id: this.userid, page: this.page})
        .then(res => {
          console.log(res)
          for (var i = 0; i < res.data.data.length; i++) {
            this.circleArr.push(res.data.data[i])
          }
          if (res.data.data.length > 0) {
            window.addEventListener('scroll', this.scrollBottom, true)
            this.isScroll = true
          } else {
            window.removeEventListener('scroll', this.scrollBottom, true)
            this.isScroll = false
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 点击发布跳转
    clickSend () {
      // 朋友圈发布 publishFriendCircle
      if (sessionStorage.getItem('sys') === 'ANDROID') {
        window.WebViewJavascriptBridge.callHandler('publishFriendCircle', {'param': ''}, function (responseData) {})
      } else {
        this.$router.push({path: '/sendCircleFriends', query: {userid: this.userid}})
      }
    }
  },
  destroyed () {
    // 用户离开，就只执行销毁监听事件
    clearTimeout(this.timer)
    window.removeEventListener('scroll', this.scrollBottom, true)
  },
  beforeDestroy () {
    window.removeEventListener('scroll', this.scrollBottom, true)
  }
}
</script>

<style scoped>
  *{
    margin: 0;
    padding: 0;
  }
  .all{
    width: 100%;
    height: 100%;
    overflow: auto;
  }
  header{
    width: 100%;
    height: 12vw;
    display: flex;
    align-items: center;
    font-size: 15px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #1DA4F2;
    background: #FFFFFF;
    position: sticky;
    top: 0;
    z-index: 99;
  }

  header div{
    width: 33%;
    display: flex;
    align-items: center;
  }
  header div:nth-child(1) img{
    width: 7px;
    height: 13px;
    margin: 0 5px 0 15px;
  }
  header div:nth-child(2){
    justify-content: center;
    color: #333;
  }
  header div:nth-child(3) img{
    width: 13px;
    height: 13px;
    margin-right: 3px;
  }
  header div:nth-child(3){
    justify-content: flex-end;
    margin-right: 15px;
  }
  .content{
    width: 100vw;
  }
  .list{
    display: flex;
    margin-bottom: 10px;
    padding: 8px 0 3px 0;
    border-top: 1px solid #e5e5e5;
  }
  .list .left_img{
    width: 20%;
    display: flex;
    align-items: flex-start;
    justify-content: center;
  }
  .list .right_content{
    width: 78%;
  }
  .list .right_content ul{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  .name{
    font-size: 15px;
    color: #1da4f2;
    margin-bottom: 10px;
  }
  .text{
    font-size: 15px;
    color: #333;
    margin-bottom: 5px;
  }
  .time{
    height: 25px;
    display: flex;
    align-items: center;
    font-size: 11px;
    color: #999999;
  }
  .mune{
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
  .comment{
    background: rgba(250, 249, 254, 1);
    border-radius: 5px;
    padding: 8px;
    font-size: 13px;
  }
  .comment p{
    line-height: 1.7;
    /*display: flex;*/
    /*align-items: center;*/
    /*margin-bottom: 8px;*/
  }
  .pinglunImg{
    width: 30px;
    height: 30px;
    object-fit: cover;
    border-radius: 50%;
    margin-right: 8px;
  }
  .comment p a{
    display: flex;
    flex-direction: column;
  }
  .comment_name{
    height: 100%;
    color: #1da4f2;
    white-space: nowrap;
    /*display: block;*/
    margin-bottom: 5px;
    /*display: flex;*/
    /*flex-direction: column;*/
  }
  .mune span{
    display: flex;
    align-items: center;
    font-size: 13px;
    margin-right: 10px;
    margin-left: 10px;
  }
  .mune img{
    width: 15px;
    height: 15px;
  }
  .list .right_content ul li{
    width: 32%;
    height: 94px;
    background: #bbeafb;
    margin-left: 3px;
    margin-bottom: 3px;
  }
  .pingLun{
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 44px;
    background: #FFFFFF;
  }
  .pingLun input{
    width: 80%;
    height: 29px;
    background: #FAF9FE;
    border-radius: 10px;
    border: none;
    padding-left: 10px;
    box-sizing: border-box;
  }
  .plSend{
    font-size: 15px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #1DA4F2;
    margin-left: 8px;
  }
  /deep/ .van-search .van-cell{
    padding: 5px 8px 5px 10px;
  }
  /deep/ .van-search{
    width: 80%;
  }
  /deep/ .van-field__value{
  }
</style>
